#pragma once

#include <memory>
#include <unistd.h>
#include "request_data.h"
#include <queue>
#include <vector>

//前向声明，否则编译器无法解析自定义类型
struct RequestData;  

class MyTimer {
public:
    MyTimer(std::shared_ptr<RequestData> requestData, int timeout); 
    ~MyTimer();

    bool IsExpired();

    bool IsDeleted();

    void SetDeleted(); 

    long GetExpTime() const;

private:
    bool deleted_; 
    long expiredTime_; //以毫秒计
    std::shared_ptr<RequestData> requestData_;
};

// 单例模式
class TimerManager{
struct TimerCmp {
    bool operator()(const std::shared_ptr<MyTimer>& a, const std::shared_ptr<MyTimer>& b) const{
        return a->GetExpTime() > b->GetExpTime();
    }
};

public:
    static TimerManager& Instance();
    ~TimerManager();

private:
    TimerManager();

public:
    void HandleTimer();
    void InsertTimer(std::shared_ptr<MyTimer>);

private:
    Mutex timerQueueLocker_;
    std::priority_queue<std::shared_ptr<MyTimer>, std::vector<std::shared_ptr<MyTimer>>, TimerCmp> timerQueue_;
};

